using UnityEngine;
using UnityEditor;
using Newtonsoft.Json.Linq;
using System.IO;
using System;
using System.Collections.Generic;

public class ParseMeshRenderer : ParseObjectHandle<MeshRenderer>
{
    public override void Prase(ExportSetting ExportSetting, MeshRenderer meshRenderer, JArray jArray)
    {
        if (meshRenderer.GetComponent<MeshFilter>().sharedMesh == null)
        {
            return;
        }

        var ccmeshrender = ExportSetting.CocosObjectConfig["cc.MeshRenderer"].DeepClone();
        var cccompPrefabInfo = ExportSetting.CocosObjectConfig["cc.CompPrefabInfo"].DeepClone(); ;
        var cclightMapSetting = ExportSetting.CocosObjectConfig["cc.ModelLightmapSettings"].DeepClone(); ;

        ccmeshrender["_name"] = "";
        ccmeshrender["_shadowCastingMode"] = meshRenderer.shadowCastingMode == UnityEngine.Rendering.ShadowCastingMode.Off ? 0 : 1;
        ccmeshrender["_shadowReceivingMode"] = meshRenderer.receiveShadows ? 1 : 0;

        var ccmeshrender_materialjarray = ccmeshrender["_materials"] as JArray;
        ccmeshrender_materialjarray.Clear();
        var materials = meshRenderer.sharedMaterials;

        if (materials.Length > 0)
        {
            foreach (var material in materials)
            {
                var matJobject = new JObject();
                matJobject["__expectedType__"] = "cc.Material";
                var matuuid = AssetHelper.GetObjectUUID(material);
                string matPath = AssetDatabase.GetAssetPath(material);
                string matPathExtension = Path.GetExtension(matPath);

                if (!Path.HasExtension(matPath) && material.name == "Default-Material")
                {
                    matJobject["__uuid__"] = ExportSetting.CocosShaderConfig["DefaultMaterialUUID"];
                }
                else
                {
                    matJobject["__uuid__"] = $"{matuuid}";
                    AssetHelper.CreateMaterial(ExportSetting, material);
                }
                ccmeshrender_materialjarray.Add(matJobject);
            }
        }

        var mesh = meshRenderer.GetComponent<MeshFilter>().sharedMesh;

        var meshPath = AssetDatabase.GetAssetPath(mesh);
        var meshExtension = Path.GetExtension(meshPath);
        if (!Path.HasExtension(meshPath))
        {
            string defaultMeshUUID = (string)ExportSetting.CocosNodeConfig["DefalutMeshUUID"][AssetHelper.GetNameWithoutId(mesh)];
            string meshName = AssetHelper.GetIdName(mesh);
            if (defaultMeshUUID != null)
            {
                ccmeshrender["_mesh"]["__uuid__"] = $"{defaultMeshUUID}";
            }
            else
            {
                var meshuuid = AssetHelper.GetObjectUUID(mesh);
                var metaNameId = AssetHelper.GetMetaIdByName($"{meshName}.mesh");
                ccmeshrender["_mesh"]["__uuid__"] = $"{meshuuid}@{metaNameId}";
                AssetHelper.CreateGltfAndMeta(ExportSetting, mesh);
            }
        }
        else
        {
            mesh.name = AssetHelper.GetIdName(mesh);
            var asset = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(meshPath);
            var assets = AssetDatabase.LoadAllAssetsAtPath(meshPath);
            if (assets.Length > 1)
            {
                var meshes = new List<Mesh>();
                foreach (var item in assets)
                {
                    if (item is Mesh tempMesh)
                    {
                        meshes.Add(tempMesh);
                    }
                }
                if (meshes.IndexOf(mesh) == 0)
                {
                    mesh.name = AssetHelper.GetIdName(asset);
                }

            }
            var meshuuid = AssetHelper.GetObjectUUID(asset);
            var metaNameId = AssetHelper.GetMetaIdByName($"{mesh.name }.mesh");
            if (meshRenderer.gameObject.name.Contains("level1 iron"))
            {
                Debug.Log("NameId1" + " " + mesh.name + " " + metaNameId);
            }
            ccmeshrender["_mesh"]["__uuid__"] = $"{meshuuid}@{metaNameId}";
            if (meshExtension == ".asset")
            {
                AssetHelper.CreateGltfAndMeta(ExportSetting, mesh);
            }
            else
            {
                AssetHelper.CreateGltfAndMeta(ExportSetting, asset as GameObject);
            }
        }


        jArray.Add(ccmeshrender);
        var newccmeshrender = jArray[jArray.Count - 1];
        cccompPrefabInfo["fileId"] = IdHelper.GetCompressGuid();
        newccmeshrender["__prefab"] = AssetHelper.CreateIdJObject(jArray.Count);
        jArray.Add(cccompPrefabInfo);
        newccmeshrender["lightmapSettings"] = AssetHelper.CreateIdJObject(jArray.Count);
        jArray.Add(cclightMapSetting);
    }
}